package unpacking.views;

import gui.util.layout.StandardForm;

import java.io.File;
import java.util.Collection;
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import unpacking.models.IOpenZipUnpacker;

public class UnpackerGUI implements IUnpackerUI
{
	private JFrame mainWindow;
	private JTextField destinationField;
	private JButton browseButton;
	private JTree fileTree;
	private JButton selectAllButton;
	private JButton deselectAllButton;
	private JButton closeButton;
	private JButton unpackButton;
	
	private Dimension preferredButtonSize = new Dimension(100, 25);
	
	public UnpackerGUI(){
		prepareMainWindow();
		StandardForm form = new StandardForm(prepareTopPanel(), 
					prepareMiddlePanel(), prepareBottomPanel());
		form.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		mainWindow.setContentPane(form.getContainer());
		mainWindow.setSize(400, 500);
		mainWindow.setLocationRelativeTo(null);
	}

	private Component prepareTopPanel(){
		destinationField = new JTextField();
		destinationField.setPreferredSize(new Dimension(0, 25));
		browseButton = new JButton("Browse...");
		browseButton.setPreferredSize(preferredButtonSize);
		browseButton.addActionListener(new FileActionListener());
		
		JPanel panel = new JPanel(new BorderLayout(5, 0));
		panel.add(preparePanel("Destination", destinationField));
		panel.add(preparePanel(null, browseButton), BorderLayout.EAST);
		
		panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
		
		return panel;
	}

	private Component prepareMiddlePanel(){
		fileTree = new JTree();
		selectAllButton = new JButton("Select all");
		selectAllButton.setPreferredSize(preferredButtonSize);
		deselectAllButton = new JButton("Deselect all");
		deselectAllButton.setPreferredSize(preferredButtonSize);
		
		JPanel panel = new JPanel(new BorderLayout(5, 0));
			JPanel treePanel = new JPanel(new BorderLayout());
			treePanel.add(new JLabel("Files to unpack:"), BorderLayout.NORTH);
			treePanel.add(new JScrollPane(fileTree));
		panel.add(treePanel);
			Box buttonPanel = new Box(BoxLayout.Y_AXIS);
			buttonPanel.add(Box.createRigidArea(new Dimension(0, 20)));
			buttonPanel.add(selectAllButton);
			buttonPanel.add(Box.createRigidArea(new Dimension(0, 10)));
			buttonPanel.add(deselectAllButton);
		panel.add(buttonPanel, BorderLayout.EAST);

		panel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
		
		return panel;			
	}

	private Component prepareBottomPanel(){
		closeButton = new JButton("Close");
		unpackButton = new JButton("Unpack");
		
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		panel.add(closeButton);
		panel.add(Box.createRigidArea(new Dimension(5, 0)));
		panel.add(unpackButton);

		panel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
		
		return panel;
	}

	private Container preparePanel(String label, JComponent comp){
		Box panel = new Box(BoxLayout.Y_AXIS);
		panel.add(Box.createVerticalGlue());

		if(label != null){
			JLabel labelText = new JLabel(label);
			labelText.setAlignmentX(JComponent.LEFT_ALIGNMENT);
			panel.add(labelText);
			// panel.add(Box.createRigidArea(gaps));
		}

		comp.setAlignmentX(JComponent.LEFT_ALIGNMENT);
		panel.add(comp);

		return panel;
	}
	
	private void prepareMainWindow(){
		mainWindow = new JFrame("OPEN Zip - Unpack");
		mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	@Override public void showGui(IOpenZipUnpacker logic){
		mainWindow.setVisible(true);
	}

	@Override public void showGui(IOpenZipUnpacker logic, Collection<File> files){
		throw new UnsupportedOperationException("Not yet implemented.");
	}
	
	/**
	 * Listener for the Browse... button.
	 */
	private class FileActionListener implements ActionListener
	{
		/**
		 * When clicked the Browse... button, a default <code>JFileChooser</code>
		 * will be displayed. After the user picks a file, the filename and file
		 * path will be displayed in the fields.
		 */
		@Override public void actionPerformed(ActionEvent e){
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			if(fileChooser.showOpenDialog(mainWindow) == JFileChooser.APPROVE_OPTION){
				File f = fileChooser.getSelectedFile();
				destinationField.setText(f.getPath());
			}
		}
	}
	
	public static void main(String[] args){
		UnpackerGUI gui = new UnpackerGUI();
		gui.showGui(null);
	}
}
